package love.yuanmeng.guetcoalsystem.service.impl;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/10/16 21:44
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import love.yuanmeng.guetcoalsystem.mapper.CBondMapper;
import love.yuanmeng.guetcoalsystem.mapper.OrderMapper;
import love.yuanmeng.guetcoalsystem.mapper.PropMapper;
import love.yuanmeng.guetcoalsystem.pojo.CBond;
import love.yuanmeng.guetcoalsystem.pojo.Order;
import love.yuanmeng.guetcoalsystem.pojo.Prop;
import love.yuanmeng.guetcoalsystem.service.CBondService;
import love.yuanmeng.guetcoalsystem.vo.PayBondVo;
import love.yuanmeng.guetcoalsystem.vo.PropIdOrderIdVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CBondServiceImpl implements CBondService {

    @Resource
    PropMapper propMapper;
    @Resource
    OrderMapper orderMapper;
    @Resource
    CBondMapper cBondMapper;

    @Override
    @Transactional
    public int updatePropAndOrderForBond(String enId, String propId, String orderId) {


        Order order = null;
        order = orderMapper.selectById(orderId);
        if (0 != Integer.valueOf(order.getStatus())) {
            //订单已经不再是“待缴纳保证金”状态,不可进行“缴纳保证金”操作
            return 0;

        }
        Prop prop = null;
        prop = propMapper.selectById(propId);


        CBond cBond = null;
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("enterprise_id", enId);
        List<CBond> cBondList = cBondMapper.selectByMap(hashMap);

        if (cBondList.size() == 1) {
            cBond = cBondList.get(0);
//            System.out.println("$$$$科学计数法："+cBond.getUnfrozenAmount());
            BigDecimal bd1 = new BigDecimal(cBond.getUnfrozenAmount());
//            System.out.println(bd1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
//


            float zi = Float.valueOf(cBond.getUnfrozenAmount());//可以动用的资金
            float sh = Integer.valueOf(prop.getBondNumber()) * prop.getSupplyQuantity() * 10000;//报价保证金
            float myCon=Float.valueOf(cBond.getContractFrozenAmount());
            System.out.println(zi + ":" + sh);
            if (zi > sh) {
                System.out.println("未冻结资金大于报价保证金，可以缴纳！");
                cBond.setUnfrozenAmount(String.valueOf(zi - sh));
                cBond.setContractFrozenAmount(String.valueOf(myCon+sh));
                prop.setPropstatus("2");
                order.setStatus("1");
                int result1 = cBondMapper.updateById(cBond);
                int result2 = propMapper.updateById(prop);
                int result3 = orderMapper.updateById(order);

                if (result1 == 1 && result2 == 1 && result3 == 1) {
                    return 1;
                } else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return 0;
                }

            } else {


                System.out.println("未冻结资金小于报价保证金，缴纳失败！");
                return 0;
            }


        } else {
            return 0;
        }


    }

    @Override
    public CBond selectCBondById(String enId) {
        QueryWrapper<CBond> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id", enId);
        return cBondMapper.selectOne(queryWrapper);
    }

    @Override
    public int updatePropAndDeleteOrder(PropIdOrderIdVo propIdOrderIdVo) {
        Order order = null;
        order = orderMapper.selectById(propIdOrderIdVo.getOrderId());
        if (0 != Integer.valueOf(order.getStatus())) {
            //订单已经不再是“待缴纳保证金”状态,不可进行“撤销”操作
            return 0;
        }
        Prop prop = null;
        prop = propMapper.selectById(propIdOrderIdVo.getPropId());
        if (1 != Integer.valueOf(prop.getPropstatus())) {
            //牌子不再是摘挂状态，不可以进行“被摘->在挂”
            return 0;
        }
        prop.setPropstatus("0");
        propMapper.updateById(prop);
        orderMapper.deleteById(order.getOrderId());
        return 1;
    }
}
